package com.baidu.service.impl;

import com.baidu.model.entity.CommentModel;
import com.baidu.model.entity.example.CommentModelExample;
import com.baidu.model.mapper.CommentModelMapper;
import com.baidu.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.until.pagination.PaginationRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author user
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentModelMapper comment;

    @Override
    public int createComment(CommentModel commentModel) throws Exception {
        return comment.insertSelective(commentModel);
    }

    @Override
    public PageInfo<CommentModel> selectByPhotoId(Integer id, PaginationRequest page, Integer commentId) {
        CommentModelExample commentModelExample = new CommentModelExample();
        if (commentId<=0) {
            commentModelExample.createCriteria().andPhotoIdEqualTo(id).andParentIdIsNull();
        }else{
            commentModelExample.createCriteria().andPhotoIdEqualTo(id).andParentIdEqualTo(commentId);
        }
        return PageHelper.startPage(page.getPageNumber(), page.getPageSize()).doSelectPageInfo(() -> comment.selectByExample(commentModelExample));
    }

    @Override
    public int deleteByPhotoId(Integer id) throws Exception {
        return comment.deleteByPrimaryKey(id);
    }
}
